/*
 * @Author: 陈心怡
 * @Date: 2024-09-18 18:08:17
 * @LastEditors: 陈心怡
 * @LastEditTime: 2024-09-20 20:33:39
 * @FilePath: \vue3-web-study\src\router\index.js
 * @Description:
 */
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../components/cq-layout/index.vue'
import { usePageStore } from '@/stores/page'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', redirect: '/index' },
        {
          path: '/index',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '橙劵首页'
          }
        },
        {
          path: '/news',
          component: () => import('@/views/news/index.vue'),
          meta: {
            title: '橙劵新闻'
          }
        },
        {
          path: '/detail/:id',
          component: () => import('@/views/detail/index.vue'),
          meta: {
            title: '橙劵新闻详情'
          }
        },
        {
          path: '/case',
          component: () => import('@/views/case/index.vue'),
          meta: {
            title: '行业案例'
          }
        },
        {
          path: 'apply',
          component: () => import('@/views/apply/index.vue'),
          meta: {
            title: '入驻申请'
          }
        },
        {
          path: 'about',
          component: () => import('@/views/about/index.vue'),
          meta: {
            title: '关于我们'
          }
        }
      ]
    },
    {
      path: '/:catchAll(.*)',
      component: () => import('@/views/errPage/index.vue')
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log('全局前置守卫:beforeEach')
  const store = usePageStore()
  if (to?.meta?.title) {
    document.title = to.meta.title
  }
  store.setRouterPath(to.path)
  next()
})

export default router
